package com.yuexunit.finance.project.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;
import com.yuexunit.finance.project.entity.RecieveNoticeEntity;
import com.yuexunit.finance.project.entity.lexicon.NoticeStateEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

@Implements(RecieveNoticeService.class)
public class RecieveNoticeServiceImpl implements RecieveNoticeService {

	@Override
	public Long createRecieveNotice(RecieveNoticeEntity entity) {
		entity.setNoticeState(NoticeStateEnum.UNCONFIRMED);
		entity.setRecieveNoticeNumber(SettingConstants.PRE_NUMBER_NOTICE_RECIEVE + RandomUtil.generateTimeNumber());
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getRecieveNoticeId();
	}

	@Override
	public void updateRecieveNotice(RecieveNoticeEntity recieveNoticeEntity) {
		RecieveNoticeEntity persist = inquireRecieveNoticeDetail(recieveNoticeEntity.getRecieveNoticeId());
		BeanUtils.copyProperties(recieveNoticeEntity, persist);
		persist.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(persist);
	}

	@Override
	public RecieveNoticeEntity inquireRecieveNoticeDetail(Long recieveNoticeId) {
		RecieveNoticeEntity entity = PersistUtil.get(RecieveNoticeEntity.class, recieveNoticeId);
		if (entity == null) {
			throw new BusinessException("未找到相应的收货通知");
		}
		return entity;
	}

	@Override
	public PageList<RecieveNoticeEntity> inquireRecieveNoticePageList(Long financeId, String financeNumber, Long logisticsCompanyId, Integer noticeState, String enterpriseName, String bulkstockName, Integer productType, PageCriteria page) {
		List<Long> financeIds2 = null;
		if (bulkstockName !=null && !"".equals(bulkstockName)) {// 若有搜索货物名称
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockEntity.class);
			if (bulkstockName !=null && !"".equals(bulkstockName)) {
				criteria.add(Restrictions.like("bulkstockName", "%" + bulkstockName + "%"));
			}
			criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
			financeIds2 = PersistUtil.get(criteria);
			if (financeIds2.isEmpty()) {
				return new PageList<>(page);
			}
		}
		
		DetachedCriteria criteria = DetachedCriteria.forClass(RecieveNoticeEntity.class);
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (noticeState != null && noticeState != 0) {
			criteria.add(Restrictions.eq("noticeState", NoticeStateEnum.valueOf(noticeState)));
		}
		if (StringUtils.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName", "%" + enterpriseName + "%"));
		}
		if(financeIds2 !=null && financeIds2.size()>0 ){
			criteria.add(Restrictions.in("financeId", financeIds2));
		}
		if (productType != null && productType != 0) {
			criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(productType)));
		}
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		criteria.add(Restrictions.ne("financeId", 0L));
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public List<RecieveNoticeEntity> inquireRecieveNoticeList(Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RecieveNoticeEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public PageList<RecieveNoticeEntity> inquireRecieveNoticeLogisticsPageList(Long financeId, String financeNumber, Long logisticsCompanyId, Integer noticeState, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RecieveNoticeEntity.class);
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (noticeState != null && noticeState != 0) {
			criteria.add(Restrictions.eq("noticeState", NoticeStateEnum.valueOf(noticeState)));
		}
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public RecieveNoticeEntity inquireNumberRecieveNotice(String recieveNoticeNumber) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RecieveNoticeEntity.class);
		criteria.add(Restrictions.eq("recieveNoticeNumber", recieveNoticeNumber));
		List<RecieveNoticeEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<RecieveNoticeEntity> inquireExchangeRecieveNotice(Long exchangePickupId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RecieveNoticeEntity.class);
		criteria.add(Restrictions.eq("exchangePickupId", exchangePickupId));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<RecieveNoticeEntity> inquireReplenishRecieveNotice(Long replenishCreditId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RecieveNoticeEntity.class);
		criteria.add(Restrictions.eq("replenishCreditId", replenishCreditId));
		return PersistUtil.get(criteria);
	}
}
